package com.fortune.service.impl;

import com.fortune.constants.enums.ProductStock;
import com.fortune.dao.DproductStockDetailInfoDao;
import com.fortune.dao.DstoreInDao;
import com.fortune.dto.in.dStoreIn.DStoreInitProduceDto;
import com.fortune.dto.in.dStoreIn.DstoreInListDto;
import com.fortune.dto.in.dStoreIn.DstoreInitAddDto;
import com.fortune.dto.in.dStoreIn.DstoreInitEditDto;
import com.fortune.dto.in.dealerWarehouseReceiptWarehouse.ConfirmStorage;
import com.fortune.dto.in.dealerWarehouseReceiptWarehouse.DealerWarehouseReceiptWarehouseDto;
import com.fortune.service.DstoreInService;
import com.fortune.vo.dStorage.DstorageInfo;
import com.fortune.vo.dStoreIn.DinitDetails;
import com.fortune.vo.dStoreIn.DstoreInInfo;
import com.fortune.vo.productStockDetailInfo.ProductStockDetailInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/5/26 0026.
 */
@Service
public class DstoreInServiceImpl implements DstoreInService {
    @Autowired
    private DstoreInDao dstoreInDao;
    @Autowired
    private DproductStockDetailInfoDao dproductStockDetailInfoDao;

    @Override
    public List<DstoreInInfo> findStoreList(DstoreInListDto dstoreInListDto, String mid, String did) {
        return dstoreInDao.findStoreList(dstoreInListDto,mid,did);
    }

    @Override
    public int findStoreListCount(String mid, String did) {
        return dstoreInDao.findStoreListCount(mid,did);
    }

    @Override
    public int queryDInitInfoIsExist(String mid, String did, String num, String storeId) {
        return dstoreInDao.queryDInitInfoIsExist(mid,did,num,storeId);
    }

    @Override
    @Transactional
    public int addDInitInfo(String mid, String did,String id,DstoreInitAddDto addDto) {
        int num = dstoreInDao.addDinitInfo(mid,did,id,addDto);
        if (num < 1){
            throw new RuntimeException("添加初始化单失败");
        }
        List<DStoreInitProduceDto> list = addDto.getProduce();
        num = dstoreInDao.addInitDetailInfo(mid,did,id,addDto.getProduce());
        if (num < 1){
            throw new RuntimeException("添加初始化单失败");
        }
        return 1;
    }

    @Override
    @Transactional
    public int editDInitInfo(String mid, String did,DstoreInitEditDto editDto) {
         dstoreInDao.editDinitInfo(mid,did,editDto);
         dstoreInDao.deleteDinitDetailInfo(mid,did,editDto.getId());
        int num = dstoreInDao.editInitDetailInfo(mid,did,editDto.getProduce(),editDto.getId());
        if (num < 1){
            throw new RuntimeException("编辑初始化单失败");
        }
        return 1;
    }

    @Override
    public int deleteDInitInfo(String mid, String did, String id) {
        return dstoreInDao.deleteDInitInfo(mid,did,id);
    }

    @Override
    public DinitDetails queryStoreInitDetails(String mid, String did, String id) {
        return dstoreInDao.queryStoreInitDetails(mid,did,id);
    }

    @Override
    public int storeInitConfirm(String mid, String did, String id) {
        int num = dstoreInDao.updateStoreInit(mid,did,id);
        if (num < 1){
            throw new RuntimeException("确认初始化单失败");
        }
        List<ProductStockDetailInfo> list = dstoreInDao.queryProductStock(mid,did,id);
        Date date = new Date();
        num = dproductStockDetailInfoDao.insertInitProductStockDetail(mid,did,date,ProductStock.CSHRK,list);
        if (num < 1){
            throw new RuntimeException("确认初始化单失败");
        }
        return 1;
    }

    @Override
    public List<DstorageInfo> findStorageList(DealerWarehouseReceiptWarehouseDto dealerDto, String mid, String did) {
        return dstoreInDao.findStorageList(dealerDto,mid,did);
    }

    @Override
    public int findStorageListCount(DealerWarehouseReceiptWarehouseDto dealerDto, String mid, String did) {
        return dstoreInDao.findStorageListCount(dealerDto,mid,did);
    }

    @Override
    public int confirmStorage(ConfirmStorage confirmStorage, String mid, String did,String chickId) {
        String id = dstoreInDao.findStorageID(confirmStorage.getNum(),mid,did);
        return dstoreInDao.confireStorage(confirmStorage,mid,did,id,chickId);

    }
}
